package cn.xiaohaoo.datacollection;

import cn.xiaohaoo.medicine.util.mongo.MongoDatabaseUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 详情页：http://www.zhongyoo.com
 */
public class MedicineDetailPageProcessor implements PageProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(MedicineDetailPageProcessor.class);

    @Override
    public void process(Page page) {
        Document document = new Document();
        String name = page.getHtml().$(".title").xpath("h1/text()").toString();
        document.append("name", name);
        Selectable content = page.getHtml().$(".con_left2").xpath("div[1]");
        Selectable p = content.xpath("div/div[3]/p");

        AtomicReference<String> compatibility = new AtomicReference<>("");
        p.nodes().forEach(v -> {
            if (!"".equals(v.xpath("p/text()").get())) {
                if (v.xpath("img").get() != null) {
                    String img = "http://www.zhongyoo.com" + v.xpath("p/img/@src").get();
                    document.append("img", img);
                    String fullName = v.xpath("p/text()").get().replaceAll(" *　*【】", "");
                    document.append("中药名", fullName);
                } else {
                    String strong = v.xpath("strong/text()").get();
                    //当前段是接上一段的
                    if (strong == null) {
                        String replace = v.xpath("p/text()").get().replace("　", "")
                            .replace(" ", "");
                        compatibility.set(compatibility + "\n" + replace);
                    } else {
                        //这是一个新段
                        String text = v.get().replaceAll("　*【*】*", "")
                            .replace("<p>", "")
                            .replace("</p>", "")
                            .replaceAll("<strong>.*/strong>", "");
                        text = text.replaceAll("<u>|</u>", "")
                            .replaceAll("<a.*?>|</a>", "")
                            .replace(" ", "");
                        if ("配伍药方".equals(strong)) {
                            compatibility.set(compatibility + text);
                        } else {
                            document.append(strong, text);
                        }
                    }
                }
            }
        });
        document.append("配伍药方", compatibility.get());
        try {
            MongoDatabaseUtils.insertDocument(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION, document);
        } catch (Exception e) {
            LOGGER.error("=====>{}出错了！", document);
        }
    }

    @Override
    public Site getSite() {
        return new Site();
    }

    public static void main(String[] args) {
        LocalDateTime begin = LocalDateTime.now();
        Spider spider = Spider.create(new MedicineDetailPageProcessor()).thread(10);
        //获取所有的集合
        MongoDatabaseUtils.queryAllCollections(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION).forEach(v -> {
            //获取当前集合下面的药名
            MongoDatabaseUtils.queryAllDocuments(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION).forEach(p -> {
                spider.addUrl(p.get("href").toString());
            });

        });
        spider.run();
        System.out.println("爬取用时：" + Duration.between(begin, LocalDateTime.now()).toMinutes() + "分钟");
    }
}
